route.test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* @vitest-environment node */
  2. import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
  3. import fs from "node:fs/promises";
  4. import os from "node:os";
  5. import path from "node:path";
  6. vi.mock("@/lib/auth/session", () => ({
  7. getSession: vi.fn(),
  8. }));
  9. import { getSession } from "@/lib/auth/session";
  10. import { GET } from "./route.js";
  11. describe("GET /api/branches/[branch]/[year]/months", () => {
  12. let tmpRoot;
  13. const originalNasRoot = process.env.NAS_ROOT_PATH;
  14. beforeEach(async () => {
  15. vi.clearAllMocks();
  16. tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-months-"));
  17. process.env.NAS_ROOT_PATH = tmpRoot;
  18. await fs.mkdir(path.join(tmpRoot, "NL01", "2024", "10"), {
  19. recursive: true,
  20. });
  21. });
  22. afterEach(async () => {
  23. process.env.NAS_ROOT_PATH = originalNasRoot;
  24. if (tmpRoot) await fs.rm(tmpRoot, { recursive: true, force: true });
  25. });
  26. it("returns 401 when unauthenticated", async () => {
  27. getSession.mockResolvedValue(null);
  28. const res = await GET(
  29. new Request("http://localhost/api/branches/NL01/2024/months"),
  30. { params: Promise.resolve({ branch: "NL01", year: "2024" }) }
  31. );
  32. expect(res.status).toBe(401);
  33. expect(await res.json()).toEqual({
  34. error: { message: "Unauthorized", code: "AUTH_UNAUTHENTICATED" },
  35. });
  36. });
  37. it("returns 403 when branch user accesses a different branch", async () => {
  38. getSession.mockResolvedValue({
  39. role: "branch",
  40. branchId: "NL01",
  41. userId: "u1",
  42. });
  43. const res = await GET(
  44. new Request("http://localhost/api/branches/NL02/2024/months"),
  45. { params: Promise.resolve({ branch: "NL02", year: "2024" }) }
  46. );
  47. expect(res.status).toBe(403);
  48. expect(await res.json()).toEqual({
  49. error: { message: "Forbidden", code: "AUTH_FORBIDDEN_BRANCH" },
  50. });
  51. });
  52. it("returns months for a valid branch/year when allowed", async () => {
  53. getSession.mockResolvedValue({
  54. role: "admin",
  55. branchId: null,
  56. userId: "u2",
  57. });
  58. const res = await GET(
  59. new Request("http://localhost/api/branches/NL01/2024/months"),
  60. { params: Promise.resolve({ branch: "NL01", year: "2024" }) }
  61. );
  62. expect(res.status).toBe(200);
  63. expect(await res.json()).toEqual({
  64. branch: "NL01",
  65. year: "2024",
  66. months: ["10"],
  67. });
  68. });
  69. it("returns 400 when branch or year is missing (authenticated)", async () => {
  70. getSession.mockResolvedValue({
  71. role: "admin",
  72. branchId: null,
  73. userId: "u2",
  74. });
  75. const res = await GET(
  76. new Request("http://localhost/api/branches/NL01//months"),
  77. { params: Promise.resolve({ branch: "NL01", year: undefined }) }
  78. );
  79. expect(res.status).toBe(400);
  80. expect(await res.json()).toEqual({
  81. error: {
  82. message: "Missing required route parameter(s)",
  83. code: "VALIDATION_MISSING_PARAM",
  84. details: { params: ["year"] },
  85. },
  86. });
  87. });
  88. it("returns 404 when the year folder does not exist (authorized)", async () => {
  89. getSession.mockResolvedValue({
  90. role: "admin",
  91. branchId: null,
  92. userId: "u2",
  93. });
  94. const res = await GET(
  95. new Request("http://localhost/api/branches/NL01/2099/months"),
  96. { params: Promise.resolve({ branch: "NL01", year: "2099" }) }
  97. );
  98. expect(res.status).toBe(404);
  99. expect(await res.json()).toEqual({
  100. error: {
  101. message: "Not found",
  102. code: "FS_NOT_FOUND",
  103. details: { branch: "NL01", year: "2099" },
  104. },
  105. });
  106. });
  107. });